package com.feng.eduservice.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.feng.commonutils.R;
import com.feng.eduservice.entity.EduCourse;
import com.feng.eduservice.mapper.EduTeacherMapper;
import com.feng.eduservice.service.EduCourseService;
import com.feng.eduservice.service.EduTeacherService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.feng.eduservice.entity.EduTeacher;
import com.feng.eduservice.dto.TeacherQuery;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 讲师 服务实现类
 * </p>
 *
 * @author fengge
 * @since 2023-01-09
 */
@Service
public class EduTeacherServiceImpl extends ServiceImpl<EduTeacherMapper, EduTeacher> implements EduTeacherService {
    @Autowired
    private EduTeacherService eduTeacherService;
    @Autowired
    private EduCourseService eduCourseService;
    @Override
    public R findAllTeacher() {
        return R.ok().data("item",eduTeacherService.list(null));
    }

    @Override
    public R removeTeacher(String id) {
        boolean flag = eduTeacherService.removeById(id);
        if(flag){
            return R.ok();
        }
        else {
            return R.error();
        }
    }

    @Override
    public R pageListTeacher(long current, long limit) {
        Page<EduTeacher> page=new Page<>(current,limit);
        this.page(page,null);
        //返回总条数
        long total = page.getTotal();
        //返回总记录数
        List<EduTeacher> records = page.getRecords();
        Map<String,Object> map=new HashMap();
        map.put("total",total);
        map.put("rows",records);
        return R.ok().data(map);
    }

    @Override
    public R pageTeacherCondition(long current, long limit, TeacherQuery teacherQuery) {
        //创建page对象
        Page<EduTeacher> page=new Page<>(current,limit);
        //调用方法分页查询数据
        LambdaQueryWrapper<EduTeacher> queryWrapper=new LambdaQueryWrapper<>();
        //构建条件
        queryWrapper.like(!StringUtils.isEmpty(teacherQuery.getName()),EduTeacher::getName,teacherQuery.getName());
        queryWrapper.eq(!StringUtils.isEmpty(teacherQuery.getLevel()),EduTeacher::getLevel,teacherQuery.getLevel());
        queryWrapper.ge(!StringUtils.isEmpty(teacherQuery.getBegin()),EduTeacher::getGmtCreate,teacherQuery.getBegin());
        queryWrapper.le(!StringUtils.isEmpty(teacherQuery.getEnd()),EduTeacher::getGmtCreate,teacherQuery.getEnd());
        queryWrapper.orderByDesc(EduTeacher::getGmtCreate) ;
        this.page(page,queryWrapper);
        //返回总条数
        long total = page.getTotal();
        //返回总记录数
        List<EduTeacher> records = page.getRecords();
        Map<String,Object> map=new HashMap();
        map.put("total",total);
        map.put("rows",records);
        return R.ok().data(map);
    }

    @Override
    public R addTeacher(EduTeacher eduTeacher) {
        boolean save = eduTeacherService.save(eduTeacher);

        if(save){
            return R.ok();
        }
        else{
            return R.error();
        }
    }

    @Override
    public R getTeacher(String id) {
        return R.ok().data("teacher",eduTeacherService.getById(id));
    }

    @Override
    public R updateTeacher(EduTeacher eduTeacher) {
        boolean b = eduTeacherService.updateById(eduTeacher);
        if(b){
            return R.ok();
        }
        else{
            return R.error();
        }
    }

    @Override
    public R getTeacherList(long page, long limit) {
        Page<EduTeacher> teacherPage=new Page<>(page,limit);
        LambdaQueryWrapper<EduTeacher> queryWrapper=new LambdaQueryWrapper<>();
        queryWrapper.orderByDesc(EduTeacher::getId);

        baseMapper.selectPage(teacherPage,queryWrapper);

        //把分页的数据获取出来返回一个map集合
        HashMap<String, Object> map = new HashMap<>();

        //总记录数
        long total = teacherPage.getTotal();
        //当前页
        long current = teacherPage.getCurrent();
        //每页记录数
        long size = teacherPage.getSize();
        //查询到的对象
        List<EduTeacher> teacherList = teacherPage.getRecords();
        //总页数
        long pages = teacherPage.getPages();
        //是否有上一页
        boolean hasPrevious = teacherPage.hasPrevious();
        //是否有下一页
        boolean hasNext = teacherPage.hasNext();
       //将数据封装到map中返回
        map.put("total",total);
        map.put("current",current);
        map.put("size",size);
        map.put("list",teacherList);
        map.put("hasPrevious",hasPrevious);
        map.put("hasNext",hasNext);
        map.put("pages",pages);

        //返回分页的所有数据
        return  R.ok().data(map);
    }

    @Override
    public R getTeacherFrontInfo(String teacherId) {
        //根据讲师id查讲师的基本信息
        EduTeacher eduTeacher = this.getById(teacherId);
        //根据讲师id查询所讲课程
         LambdaQueryWrapper<EduCourse> queryWrapper=new LambdaQueryWrapper<>();
         queryWrapper.eq(EduCourse::getTeacherId,teacherId);
         //显示已发布课程
         queryWrapper.eq(EduCourse::getStatus,"Normal");
        List<EduCourse> list = eduCourseService.list(queryWrapper);
        return R.ok().data("teacher",eduTeacher).data("courseList",list);
    }
}
